package com.broada.apm.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.broada.apm.dao.ResourceDao;
import com.broada.apm.model.PagingList;
import com.broada.apm.model.Resource;

public class MysqlResourceDao extends MysqlAbstractDao implements ResourceDao {

	public static final String RESOURCE_NAMESPACE = "com.broada.apm.dao.ResourceDao.";
	
	@Override
	public void addResource(Resource resource) {
		try {
			if(resource != null && getResourceById(resource.getId()) == null) {
				getSqlSession().insert(RESOURCE_NAMESPACE + "addResource", resource);
				if(resource.getSoapFunction() != null) {
					getSqlSession().insert(RESOURCE_NAMESPACE + "addResourceSoapFunction", resource);	
				}
			} else { 
//				updateResource(resource);
			}
		} catch (Exception e) {
		}
	}

//	@Override
//	public void updateResource(Resource resource) {
//		getSqlSession().update(RESOURCE_NAMESPACE + "updateResource", resource);
//	}

	@Override
	public void deleteResource(Resource resource) {
		getSqlSession().delete(RESOURCE_NAMESPACE + "deleteResource", resource);
	}

	@Override
	public Resource getResourceById(String resId) {
		return getSqlSession().selectOne(RESOURCE_NAMESPACE + "getResourceById", resId);
	}

	@Override
	public List<Resource> getResourcesByAppId(Resource resource) {
		return getSqlSession().selectList(RESOURCE_NAMESPACE + "getResourcesByAppId", resource);
	}

	@Override
	public List<Resource> getAllResources() {
		PagingList<Resource> page = new PagingList<Resource>();
		List<Resource> resources =  getSqlSession().selectList(RESOURCE_NAMESPACE + "getAllResources", page);
		return resources;
	}

	@Override
	public List<Resource> getResourcesByCondition(Resource resource) {
		 return getSqlSession().selectList(RESOURCE_NAMESPACE + "getResourcesByCondition", resource);
	}

	@Override
	public List<Resource> getResourceByName(String txName) {
		Resource resource = new Resource();
		resource.setUri(txName);
		return getSqlSession().selectList(RESOURCE_NAMESPACE + "getResourceByName", resource);
	}

	@Override
	public List<Resource> getResourceByIds(List<String> resourceIds) {
		if (resourceIds != null && !resourceIds.isEmpty()) {
			String[] resourceIdArray = resourceIds.toArray(new String[resourceIds.size()]);
			return getSqlSession().selectList(RESOURCE_NAMESPACE + "getResourceByIds", resourceIdArray);
		}
		return null;
	}

	@Override
	public void addKeyResource(Resource resource) {
		try {
			if(resource != null && getResourceById(resource.getId()) == null) {
				getSqlSession().insert(RESOURCE_NAMESPACE + "addKeyResource", resource);
				if(resource.getSoapFunction() != null) {
					getSqlSession().insert(RESOURCE_NAMESPACE + "addResourceSoapFunction", resource);	
				}
			} else { 
				updateKeyResource(resource);
			}
		} catch (Exception e) {
		}
	}

	@Override
	public void updateKeyResource(Resource resource) {
		getSqlSession().update(RESOURCE_NAMESPACE + "updateKeyResource", resource);
	}

	@Override
	public Resource getKeyResourceById(String id) {
		return getSqlSession().selectOne(RESOURCE_NAMESPACE + "getKeyResourceById", id);
	}

	@Override
	public List<Resource> getKeyResourcesByCondition(Resource resource) {
		return getSqlSession().selectList(RESOURCE_NAMESPACE + "getKeyResourcesByCondition", resource);
	}

	@Override
	public List<Resource> getKeyResourceByIds(List<String> resourceIds) {
		if (resourceIds != null && !resourceIds.isEmpty()) {
			String[] resourceIdArray = resourceIds.toArray(new String[resourceIds.size()]);
			return getSqlSession().selectList(RESOURCE_NAMESPACE + "getKeyResourceByIds", resourceIdArray);
		}
		return null;
	}

	@Override
	public List<Resource> getKeyResourcesByAppId(Resource resource) {
		return getSqlSession().selectList(RESOURCE_NAMESPACE + "getKeyResourcesByAppId", resource);
	}

	@Override
	public List<Resource> getKeyResourceByUri(String txName, String appId, String serviceId) {
		Resource resource = new Resource();
		resource.setUri(txName);
		resource.setApplicationId(appId);
		resource.setBelongTo(serviceId);
		return getSqlSession().selectList(RESOURCE_NAMESPACE + "getKeyResourceByUri", resource);
	}

	@Override
	public List<String> getKeyResourceIdsByCondition(Resource resource) {
		List<String> resourceIds = new ArrayList<String>();
		List<Resource> resourceList = getSqlSession().selectList(RESOURCE_NAMESPACE + "getKeyResourceIdsByCondition", resource);
		for (Resource re : resourceList) {
			resourceIds.add(re.getId());
		}
		return resourceIds;
	}

	@Override
	public List<Resource> getResourcesByKeyWord(Resource resource) {
		return getSqlSession().selectList(RESOURCE_NAMESPACE + "getResourcesByKeyWord", resource);
	}

}
